from utils.pd_show_utils import df_table
from ..base import BaseCrawler
import pandas as pd
import qstock as qs
import os

class Spider_qs_financial_statement_parser(BaseCrawler):
    def __init__(self, name,url, data_process_type,params):
        super().__init__(name,url,data_process_type, params)

    def fetch(self):
        method_name,arg = self.url.split('-')
        method = getattr(qs, method_name)
        pd_result = method(arg)
        return pd_result

    def parse(self, df):
        """解析原始数据"""
        if "利润表" in self.url:
            df['净利润'] = round(df['净利润']/100000000, 4)
            df['营业总收入'] = round(df['营业总收入']/100000000, 4)
            df['营业支出'] = round(df['营业支出']/100000000, 4)
            df['销售费用'] = round(df['销售费用']/100000000, 4)
            df['管理费用'] = round(df['管理费用']/100000000, 4)
            df['财务费用'] = round(df['财务费用']/100000000, 4)
            df['营业利润'] = round(df['营业利润']/100000000, 4)
            df['营业总支出'] = round(df['营业总支出']/100000000, 4)
            df['利润总额'] = round(df['利润总额']/100000000, 4)

            df.rename(columns={'净利润': '净利润(亿)', '营业总收入': '营业总收入(亿)',
            '营业支出': '营业支出(亿)', '销售费用': '销售费用(亿)',
            '管理费用': '管理费用(亿)', '财务费用': '财务费用(亿)',
            '营业总支出': '营业总支出(亿)', '利润总额': '利润总额(亿)', '营业利润': '营业利润(亿)',}, inplace=True)
        elif "现金流量" in self.url:
            df['净现金流'] = round(df['净现金流']/100000000, 4)
            df['经营性现金流量净额'] = round(df['经营性现金流量净额']/100000000, 4) 
            df['投资性现金流量净额'] = round(df['投资性现金流量净额']/100000000, 4)
            df['融资性现金流量净额'] = round(df['融资性现金流量净额']/100000000, 4)
            df.rename(columns={'净现金流': '净现金流(亿)', '经营性现金流量净额': '经营性现金流量净额(亿)',
            '投资性现金流量净额': '投资性现金流量净额(亿)', '融资性现金流量净额': '融资性现金流量净额(亿)'}, inplace=True)
        elif "资产负债表" in self.url:
            df['货币资金'] = round(df['货币资金']/100000000, 3)
            df['应收账款'] = round(df['应收账款']/100000000, 3)
            df['存货'] = round(df['存货']/100000000, 3)
            df['总资产'] = round(df['总资产']/100000000, 3)
            df['应付账款'] = round(df['应付账款']/100000000, 3)
            df['预收账款'] = round(df['预收账款']/100000000, 3)
            df['总负债'] = round(df['总负债']/100000000, 3)
            df['股东权益'] = round(df['股东权益']/100000000, 3)
            df.rename(columns={'货币资金': '货币资金(亿)', '总资产': '总资产(亿)',
            '应收账款': '应收账款(亿)', '存货': '存货(亿)',
            '应付账款': '应付账款(亿)', '预收账款': '预收账款(亿)',
            '总负债': '总负债(亿)', '股东权益': '股东权益(亿)',}, inplace=True)
        return df

